package com.nit.store.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.nit.store.mapper.AddressMapper;
import com.nit.store.mapper.DistrictMapper;
import com.nit.store.pojo.dto.output.AddressDto;
import com.nit.store.pojo.entity.Address;
import com.nit.store.service.AddressService;
import com.nit.store.util.CommonUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.apache.commons.lang3.StringUtils;

import java.util.List;

/**
 * @Author J1ay
 * @Date 2022/7/21 19:00
 * @Description
 */
@Slf4j
@Service
public class AddressServiceImpl  implements AddressService {

    @Autowired
    private AddressMapper addressMapper;
    @Autowired
    private DistrictMapper districtMapper;

    /**
     * 地址管理
     * @param uid
     * @return
     */
    @Override
    public List<AddressDto> getAddress(Long uid) {
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid).orderByDesc("is_default");
        List<Address> data = addressMapper.selectList(wrapper);
        return CommonUtils.getMapperFacade().mapAsList(data, AddressDto.class);
    }



    @Override
    public void setDefault(long aid, long uid) {
        UpdateWrapper<Address> wrapper = new UpdateWrapper<>();
        wrapper.set("is_default", 0)
                .eq("uid", uid)
                .eq("is_default", 1);
        addressMapper.update(null, wrapper);

        wrapper.clear();

        wrapper.set("is_default", 1)
                .eq("aid", aid);
        addressMapper.update(null, wrapper);
    }

    @Override
    public void delete(long aid) {
        addressMapper.deleteById(aid);
    }

    @Override
    public void add(String receiver, String provinceCode, String cityCode, String areaCode, String zip, String address, String phone, String tel, String tag, String username, long uid) {
        Address addressEntity = new Address();
        // 收货人
        addressEntity.setReceiver(receiver);
        // 省代号、省名称
        addressEntity.setProvinceCode(provinceCode);
        System.out.println("code" + provinceCode);
        System.out.println("name" + districtMapper.getNameByCode(provinceCode));
        addressEntity.setProvinceName(districtMapper.getNameByCode(provinceCode));
        // 城市代号名称
        addressEntity.setCityCode(cityCode);
        addressEntity.setCityName(districtMapper.getNameByCode(cityCode));
        // 区名称区代号
        addressEntity.setAreaCode(areaCode);
        addressEntity.setAreaName(districtMapper.getNameByCode(areaCode));
        // 邮政编码（可不填）
        if (! StringUtils.isBlank(zip)) {
            addressEntity.setZip(zip);
        }
        addressEntity.setAddress(address);
        addressEntity.setPhone(phone);
        // 固定电话可不填
        if (! StringUtils.isBlank(tel)) {
            addressEntity.setTel(tel);
        }

        // 地址类型 可不填
        if (! StringUtils.isBlank(tag)) {
            addressEntity.setTag(tag);
        }
        addressEntity.setUid(uid);
        addressEntity.setCreatedUser(username);
        addressEntity.setModifiedUser(username);
        QueryWrapper<Address> wrapper = new QueryWrapper<>();
        wrapper.eq("uid", uid);
        addressEntity.setIsDefault(addressMapper.selectCount(wrapper) == 0 ? 1L : 0L);
        addressMapper.insert(addressEntity);
    }
}
